﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;
//abc
namespace LoginServer
{
    public partial class LoginServer
    {
        public void OutRoom(int UserID, int RoomID)
        {
            if(manager_user.CheckUser(UserID)){
                if (manager_room.CheckRoomID(RoomID))
                {
                    Room room = manager_room.GetRoom(RoomID);
                    if (room.CheckUserInRoom(UserID))
                    {
                        User user = manager_user.GetUser(UserID);
                        AccessDB.OutRoom(RoomID, UserID);//csdl
                        room.OutRoom(UserID);//manager room
                        user.info.RoomID = -1;
                        foreach (DictionaryEntry tmp in manager_user.getAllUser())
                        {
                            User tmp2 = (User)tmp.Value;
                            try
                            {
                                tmp2.SendMessage_L_OutRoom_OK(room.Str_OutRoom() + "|" + UserID.ToString());
                                if (tmp2.info.RoomID == RoomID)
                                {
                                    tmp2.SendMessage_L_UpdateOutRoom_OK(user.Str_OutRoom());
                                }
                            }
                            catch { }
                        }
                        Console.WriteLine("OutRoom, {0} UserInRoom", room.Manager_Player.Players.Count);
                    }
                }
            }
        }
        public void OutTable(int UserID, int TableID)
        {
            if (manager_user.CheckUser(UserID))
            {
                if (manager_room.CheckRoomID(manager_user.GetUser(UserID).info.RoomID))
                {
                    Room room = manager_room.GetRoom(manager_user.GetUser(UserID).info.RoomID);
                    if (room.CheckUserInRoom(UserID))
                    {
                        if (room.CheckTableInRoom(TableID))
                        {
                            if (room.CheckUserInTable(UserID))
                            {
                                Table table = room.Manager_Table.GetTable(TableID);
                                AccessDB.OutTable(TableID, UserID);//csdl
                                manager_user.GetUser(UserID).info.TableID = -1;
                                if (room.Manager_Table.CheckTableID(TableID))
                                    if (room.Manager_Table.GetTable(TableID).info.PlayerID2 == UserID)
                                    {
                                        room.OutTable(TableID, UserID);
                                        foreach (var i in room.Manager_Player.Players)
                                        {
                                            User tmp2 = manager_user.GetUser(i);
                                            if (manager_room.CheckRoomID(tmp2.info.RoomID))
                                            {
                                                try
                                                {
                                                    tmp2.SendMessage_L_RecurrenceTable_OK(table.Str_RecurrenceTable() + "|" + UserID.ToString());
                                                }
                                                catch { }
                                            }
                                        }
                                        Console.WriteLine("RecurrenceTable");
                                    }
                                    else//out table
                                    {
                                        //room.OutTable(TableID, UserID);
                                        foreach (var i in room.Manager_Player.Players)
                                        {
                                            User tmp2 = manager_user.GetUser(i);
                                            if (manager_room.CheckRoomID(tmp2.info.RoomID))
                                                try
                                                {
                                                    tmp2.SendMessage_L_OutTable_OK(table.Str_OutTable() + "|" + UserID.ToString());
                                                }
                                                catch { }
                                        }
                                        ManagerMachine machine = GetMachineHasTable(TableID);
                                        AccessDB.RemoveTable(TableID);
                                        room.RemoveTable(table);
                                        machine.SendMessage_L_Remove_Table(TableID);
                                        machine.RemoveTable(TableID);//manager machine
                                        Console.WriteLine("OutTable");
                                    }
                                Console.WriteLine("OutTable, {0} users in table", room.Manager_Player.PlayerInTable.Count);
                                Console.WriteLine("--------, {0} tables", room.Manager_Table.tables.Count);
                            }
                        }
                    }
                }
            }
        }

        internal void JoinRoomF(int UserID, int UserCreateID)
        {
            if (manager_user.CheckUser(UserCreateID))
                if (manager_user.CheckUser(UserID))
                {
                    if (manager_room.CheckRoomID(manager_user.GetUser(UserCreateID).info.RoomID))
                    {
                        Room room = manager_room.GetRoom(manager_user.GetUser(UserCreateID).info.RoomID);
                        if (room.CheckUserInRoom(UserCreateID))
                        {
                            if (room.CheckUserOutRoom(UserID))
                            {
                                User userjoin = manager_user.GetUser(UserID);
                                int RoomID = AccessDB.GetRoom(UserCreateID);
                                JoinRoom(UserID, RoomID);
                            }
                        }
                    }
                }
        }
        public void CreateRoom(int UserID, int ServerID, string RoomName)
        {
            if (manager_user.CheckUser(UserID))
            {
                if (manager_room.CheckRoomOutID(manager_user.GetUser(UserID).info.RoomID))
                {
                    ManagerMachine machine;
                    if (ServerID != -1)
                    {
                        machine = list_manager_machine[ServerID];
                    }
                    else
                    {
                        machine = distributed.GetMinRoom(list_manager_machine);
                    }

                    int RoomID = manager_room.GetNewID();
                    machine.SendMessage_L_CreateRoom(UserID, RoomID, RoomName);
                }
            }
        }
        public void CreatTable(int UserCreatID, int RoomID)
        {
            if (manager_user.CheckUser(UserCreatID))
            {
                //ManagerMachine min = distributed.GetMinTable(list_manager_machine);
                if (manager_room.CheckRoomID(RoomID))
                {
                    Room room = manager_room.GetRoom(RoomID);
                    if (room.CheckUserInRoom(UserCreatID))
                    {
                        if (room.CheckUserOutTable(UserCreatID))
                        {
                            ManagerMachine machine = distributed.GetRoomOwn(list_manager_machine, RoomID);
                            ManagerTable manager_table = manager_room.GetRoom(manager_user.GetUser(UserCreatID).info.RoomID).Manager_Table;
                            int TableID = manager_table.GetNewID();
                            machine.SendMessage_L_CreateTable(UserCreatID, TableID);
                        }
                    }
                }
            }
        }
        public void JoinTable(int UserID, int TableID)
        {
            if (manager_user.CheckUser(UserID))
            {
                if (manager_room.CheckRoomID(manager_user.GetUser(UserID).info.RoomID))
                {
                    Room room = manager_room.GetRoom(manager_user.GetUser(UserID).info.RoomID);
                    if (room.CheckUserInRoom(UserID))
                    {
                        if (room.CheckUserOutTable(UserID))
                        {
                            User userjoin = manager_user.GetUser(UserID);
                            ManagerTable manager_table = manager_room.GetRoom(manager_user.GetUser(UserID).info.RoomID).Manager_Table;
                            if (manager_table.CheckTableID(TableID))
                            {
                                if (manager_table.Check_Player_Table(TableID)==1)
                                {
                                    ManagerMachine machine = GetMachineHasTable(TableID);
                                    machine.SendMessage_L_JoinTable(UserID, TableID);
                                }
                                else if (manager_table.Check_Player_Table(TableID) == -2)
                                {//sua loi join table
                                    try
                                    {
                                        Table table = room.Manager_Table.GetTable(TableID);
                                        foreach (var i in room.Manager_Player.Players)
                                        {
                                            User tmp2 = manager_user.GetUser(i);
                                            if (manager_room.CheckRoomID(tmp2.info.RoomID))
                                                try
                                                {
                                                    tmp2.SendMessage_L_OutTable_OK(table.Str_OutTable() + "|" + UserID.ToString());
                                                }
                                                catch { }
                                        }
                                        ManagerMachine machine = GetMachineHasTable(TableID);
                                        AccessDB.RemoveTable(TableID);
                                        room.RemoveTable(table);
                                        machine.SendMessage_L_Remove_Table(TableID);
                                        machine.RemoveTable(TableID);//manager machine
                                        Console.WriteLine("Sua loi join table");
                                    }
                                    catch { Console.WriteLine("error Sua loi join table"); }
                                }
                            }
                        }
                    }
                }
            }
        }
        public void JoinRoom(int UserID, int RoomID)
        {
            if (manager_user.CheckUser(UserID))
            {
                if (manager_room.CheckRoomID(RoomID))
                {
                    Room room = manager_room.GetRoom(RoomID);
                    if (room.CheckUserOutRoom(UserID))
                    {
                        User userjoin = manager_user.GetUser(UserID);
                        if (manager_room.CheckRoomID(RoomID))
                        {
                            if (manager_room.CheckNotFullPlayerInRoom(RoomID))
                            {
                                User userJoin = manager_user.GetUser(UserID);
                                ManagerMachine machine = GetMachineHasRoom(RoomID);
                                machine.SendMessage_L_JoinRoom(UserID, RoomID);
                            }
                        }
                    }
                }
                else
                {//sua loi join room
                    try
                    {
                        Room room = manager_room.GetRoom(RoomID);
                        User user = manager_user.GetUser(UserID);
                        AccessDB.OutRoom(RoomID, UserID);//csdl
                        room.OutRoom(UserID);//manager room
                        user.info.RoomID = -1;
                        foreach (DictionaryEntry tmp in manager_user.getAllUser())
                        {
                            User tmp2 = (User)tmp.Value;
                            try
                            {
                                tmp2.SendMessage_L_OutRoom_OK(room.Str_OutRoom() + "|" + UserID.ToString());
                                if (tmp2.info.RoomID == RoomID)
                                {
                                    tmp2.SendMessage_L_UpdateOutRoom_OK(user.Str_OutRoom());
                                }
                            }
                            catch { }
                        }
                        Console.WriteLine("Sua loi join room, {0} UserInRoom", room.Manager_Player.Players.Count);
                    }
                    catch { Console.WriteLine("error Sua loi join room"); }
                }
            }
        }
        //*********************************XU LY MESSAGE FROM MACHINE*****************
        public void CreatTableOK(int UserCreatID, int TableID, byte MachineID, byte[] IPAndPort)
        {
            Table table = new Table(new TableInfo(TableID, UserCreatID, manager_user.GetUser(UserCreatID).User_Name));
            Room room = manager_room.GetRoom(manager_user.GetUser(UserCreatID).info.RoomID);
            room.CreateTableOK(UserCreatID, table);

            ManagerMachine machine = GetMachine(MachineID);
            machine.AddTable(TableID);//manager machine
            User userCreate = manager_user.GetUser(UserCreatID);
            userCreate.info.TableID = TableID;
            AccessDB.CreatTable(TableID, userCreate.User_ID);//csdl
            foreach (var i in manager_room.GetRoom(userCreate.info.RoomID).Manager_Player.Players)
            {
                User tmp2 = manager_user.GetUser(i);
                if (manager_room.CheckRoomID(tmp2.info.RoomID))
                    try
                    {
                        tmp2.SendMessage_L_CreatTable_OK(table.Str_CreateTable() + "|" + UserCreatID.ToString() + ":", IPAndPort);
                    }
                    catch { }
            }
            Console.WriteLine("CreateTable, {0} user in table", room.Manager_Player.PlayerInTable.Count);
            Console.WriteLine("-----------, {0} tables", room.Manager_Table.tables.Count);
        }
        public void CreateRoomOK(int UserCreateId, int RoomID, byte MachineID, string RoomName)
        {
            Room room = new Room(new RoomInfo(RoomID, MachineID, RoomName, 1));
            manager_room.AddRoom(room);//manager room
            ManagerMachine machine = GetMachine(MachineID);
            machine.AddRoom(RoomID);//manager machine
            AccessDB.CreateRoom(room.info);//csdl
            foreach (DictionaryEntry tmp in manager_user.getAllUser())
            {
                User tmp2 = (User)tmp.Value;
                try
                {
                    tmp2.SendMessage_L_CreateRoom_OK(room.Str_CreateRoom());
                }
                catch { }
            }
            Console.WriteLine("CreateRoom, {0} rooms", manager_room.Rooms().Count);
        }
        public void JoinTableOK(int UserJoinID, int TableJoinID, byte MachineID, byte[] IPAndPort)
        {
            AccessDB.JoinTable(TableJoinID, UserJoinID);//csdl
            Room room = manager_room.GetRoom(manager_user.GetUser(UserJoinID).info.RoomID);
            room.JoinTableOK(TableJoinID, UserJoinID);
            

            Table table = room.Manager_Table.GetTable(TableJoinID);
            User userJoin = manager_user.GetUser(UserJoinID);
            userJoin.info.TableID = TableJoinID;
            foreach (var i in manager_room.GetRoom(userJoin.info.RoomID).Manager_Player.Players)
            {
                User tmp2 = manager_user.GetUser(i);
                if (tmp2.info.RoomID == userJoin.info.RoomID)
                    if (manager_room.CheckRoomID(tmp2.info.RoomID))
                        try
                        {
                            tmp2.SendMessage_L_JoinTable_OK(table.Str_JoinTable() + "|" + UserJoinID.ToString() + ":", IPAndPort);
                        }
                        catch { }
            }
            //foreach (DictionaryEntry tmp in manager_user.getAllUser())
            //{
            //    User tmp2 = (User)tmp.Value;
            //    if (manager_user.GetUser(tmp2.User_ID).RoomID == userJoin.RoomID)
            //        tmp2.SendMessage_L_JoinTable_OK(table.Str_JoinTable() + "|" + UserJoinID.ToString() + ":", IPAndPort);
            //}
            Console.WriteLine("JoinTable, {0} users in table", room.Manager_Player.PlayerInTable.Count);
            Console.WriteLine("---------, {0} table", room.Manager_Table.tables.Count);
        }
        public void JoinRoomOK(int UserJoinID, int RoomJoinID, byte MachineID)
        {
            AccessDB.JoinRoom(RoomJoinID, UserJoinID);//csdl
            Room room = manager_room.GetRoom(RoomJoinID);
            room.JoinRoomOK(UserJoinID);

            User userJoin = manager_user.GetUser(UserJoinID);
            userJoin.info.RoomID = RoomJoinID;
            foreach (DictionaryEntry tmp in manager_user.getAllUser())
            {
                User tmp2 = (User)tmp.Value;
                tmp2.SendMessage_L_JoinRoom_OK(room.Str_JoinRoom()+"|"+ UserJoinID);
                if (tmp2.info.RoomID == userJoin.info.RoomID)
                {
                    try
                    {
                        tmp2.SendMessage_L_UpdateJoinRoom_OK(userJoin.Str_JoinRoom());
                    }
                    catch { }
                }
            }
            Console.WriteLine("JoinRoom, {0} users", room.Manager_Player.Players.Count);
        }
    }
}
